package kr.hkit.powerjava.chapter14.work;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.SystemColor;

public class Bank extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JPanel upInputPanel;
	private ResultField resultPanel;
	private JPanel downInputPanel;
	private JPanel buttonPanel;
	private InputPanel upInput;
	private InputPanel downInput;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JButton btnConverter;
	private JLabel lbWon;
	private JLabel lbPer;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Bank frame = new Bank();
					frame.setVisible(true);
					frame.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Bank() {
		initialize();
	}
	private void initialize() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(Bank.class.getResource("/com/sun/java/swing/plaf/windows/icons/HardDrive.gif")));
		setTitle("이자 계산기");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 200);
		contentPane = new JPanel();
		contentPane.setBackground(SystemColor.activeCaption);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new GridLayout(0,1 , 0, 0));
		
		upInputPanel = new JPanel();
		upInputPanel.setBackground(SystemColor.inactiveCaption);
		FlowLayout fl_upInputPanel = (FlowLayout) upInputPanel.getLayout();
		fl_upInputPanel.setAlignment(FlowLayout.LEFT);
		contentPane.add(upInputPanel);
		
		lblNewLabel = new JLabel("   원금을");
		lblNewLabel.setBackground(SystemColor.inactiveCaption);
		upInputPanel.add(lblNewLabel);
		
		upInput = new InputPanel();
		upInput.setBackground(SystemColor.inactiveCaption);
		FlowLayout fl_upInput = (FlowLayout) upInput.getLayout();
		fl_upInput.setHgap(5);
		upInputPanel.add(upInput);
		
		lbWon = new JLabel("만원");
		upInputPanel.add(lbWon);
		
		downInputPanel = new JPanel();
		downInputPanel.setBackground(SystemColor.inactiveCaption);
		FlowLayout fl_downInputPanel = (FlowLayout) downInputPanel.getLayout();
		fl_downInputPanel.setAlignment(FlowLayout.LEFT);
		contentPane.add(downInputPanel);
		
		lblNewLabel_1 = new JLabel("   이율을");
		downInputPanel.add(lblNewLabel_1);
		
		downInput = new InputPanel();
		downInput.setBackground(SystemColor.inactiveCaption);
		FlowLayout fl_downInput = (FlowLayout) downInput.getLayout();
		fl_downInput.setHgap(5);
		downInputPanel.add(downInput);
		
		lbPer = new JLabel("%");
		downInputPanel.add(lbPer);
		
		buttonPanel = new JPanel();
		buttonPanel.setBackground(SystemColor.inactiveCaption);
		FlowLayout fl_buttonPanel = (FlowLayout) buttonPanel.getLayout();
		fl_buttonPanel.setVgap(10);
		contentPane.add(buttonPanel);
		
		btnConverter = new JButton("변환");
		btnConverter.addActionListener(this);
		btnConverter.setBackground(Color.ORANGE);
		buttonPanel.add(btnConverter);
		
		resultPanel = new ResultField();
		resultPanel.setBackground(SystemColor.inactiveCaption);
		contentPane.add(resultPanel);
		pack();
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("변환")) {
			double up = 0;
			double down = 0;
			try {
				up = Double.parseDouble(upInput.getInputField().trim());
			} catch(NumberFormatException err) {
				JOptionPane.showMessageDialog(this, "숫자만 입력가능합니다.");
				upInput.setInputField("");
			}
			
			try {
				down = Double.parseDouble(downInput.getInputField().trim());
			} catch(NumberFormatException err) {
				JOptionPane.showMessageDialog(this, "숫자만 입력가능합니다.");
				downInput.setInputField("");
			}
			resultPanel.setTextField("이자는 연 "+((int)(up*down))/100+"만원 입니다.");
		}
	}
}
